<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/modelo/bean/Inscricao.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/modelo/bean/Jovem.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/modelo/bean/Usuario.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/modelo/dao/InscricaoDAO.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/controller/AbstractController.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/utils/System.php'; ?>
<?php include_once $_SERVER["DOCUMENT_ROOT"] . '/JuventudeSlz/utils/Niveis.php'; ?>
<?php

/**
 * Description of ControleInscricao
 *
 * @author JoasRamos
 */
class ControleInscricao extends AbstractController {

    private $bean, $jovem, $usuario, $dao;

    public function __construct($action) {
        $this->bean = new Inscricao();
        $this->dao = new InscricaoDAO();
        $this->jovem = new Jovem(); //$_GET['id_jovem'];
        $this->usuario = new Usuario();
        parent::__construct($action);
    }

    public function create() {
        if ($this->checkRoles(Niveis::$ADMINISTRADOR)) {
            if (isset($_GET['id_jovem'])) {
                $i = $this->carregaInscricao();
                if (count($this->dao->findAll($this->bean)) > 0) {
                    if ($this->dao->insertNext($this->bean, array($this->jovem->getId(),
                                $this->usuario->getId(), $i->getData(), $i->getStatus()))) {
                        System::showViewParameter("../view/painel_admin", array("option", "filter"), array("inscricao-participante", "ativas"));
                    } else {
                        System::showViewParameter("../view/erro_page", array("erro_desc"), array("no-insert-item"));
                    }
                } else {
                    $digito = 100;
                    $init_code = ($this->mountCode() * $digito) + 1;
                    $i->setId($init_code);
                    if ($this->dao->insertFirst($this->bean, array($i->getId(), $this->jovem->getId(),
                                $this->usuario->getId(), $i->getData(), $i->getStatus()))) {
                        System::showViewParameter("../view/painel_admin", array("option", "filter"), array("inscricao-participante", "ativas"));
                    } else {
                        System::showViewParameter("../view/erro_page", array("erro_desc"), array("no-insert-item"));
                    }
                }
            } else {
                System::showViewParameter("../view/erro_page", array("erro_desc"), array("no-permited"));
            }
        } else {
            System::showViewParameter("../view/erro_page", array("erro_desc"), array("no-permission"));
        }
    }

    public function carregaInscricao() {
        $i = $this->bean;

        //montar objeto
//        $id = $this->mountCode();

        $this->jovem->setId($_GET['id_jovem']);
        $this->usuario->setId($_SESSION['current_user']['id']);
//        $data = date("d/m/Y");//date("d/m/y  H:i:s", time());
        date_default_timezone_set('America/Sao_Paulo');
        $data = date("Y/m/d H:i:s");
        $status = 1;

//        $i->setId($id);
//        $i->setJovem($jovem);
//        $i->setUsuario($usuario);
        $i->setData($data);
        $i->setStatus($status);

        return $i;
    }

    public function mountCode() {
        $datas = getdate();
        $ano = $datas["year"];
        $mes = $datas["mon"];
        $dia = $datas["mday"];

        if (strlen($dia) == 1) {
            $dia = '0' . $dia;
        }

        $ini_codigo = $ano . $mes . $dia;

        return $ini_codigo;
    }

    public function buscarTodas($id) {
        return $this->dao->findAtivas($id);
    }

    public function delete() {
        
    }

    public function personalizeAction($action) {
        
    }

    public function retrieve($id) {
        
    }

    public function findPersonalize($field, $value) {
        return $this->dao->filterBy($field, $value);
    }

}

if (isset($_REQUEST['action_code'])) {
    $action = $_REQUEST['action_code'];
    new ControleInscricao($action);
}
?>
